package com.lineage.server.datatables.sql;

import com.lineage.DatabaseFactoryLogin;
import com.lineage.server.datatables.storage.EzpayStorage;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class EzpayTable implements EzpayStorage {
  private static final Log _log = LogFactory.getLog(EzpayTable.class);
  
  public Map<Integer, int[]> ezpayInfo(String loginName) {
    Connection co = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    Map<Integer, int[]> list = (Map)new HashMap<>();
    try {
      co = DatabaseFactoryLogin.get().getConnection();
      String sqlstr = "SELECT * FROM `w_玩家即时领取物品` WHERE `玩家帐号`=? ORDER BY `流水号`";
      ps = co.prepareStatement("SELECT * FROM `w_玩家即时领取物品` WHERE `玩家帐号`=? ORDER BY `流水号`");
      ps.setString(1, loginName.toLowerCase());
      rs = ps.executeQuery();
      while (rs.next()) {
        int[] value = new int[3];
        int out = rs.getInt("领取状态");
        int ready = rs.getInt("是否手动发放");
        if (out == 0 && ready == 1) {
          int key = rs.getInt("流水号");
          int p_id = rs.getInt("发送物品编号");
          int count = rs.getInt("发送物品数量");
          value[0] = key;
          value[1] = p_id;
          value[2] = count;
          list.put(Integer.valueOf(key), value);
        } 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(co);
      SQLUtil.close(rs);
    } 
    return list;
  }
  
  private boolean is_holding(String loginName, int id) {
    Connection co = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      co = DatabaseFactoryLogin.get().getConnection();
      String sqlstr = "SELECT * FROM `w_玩家即时领取物品` WHERE `玩家帐号`=? AND `流水号`=?";
      ps = co.prepareStatement("SELECT * FROM `w_玩家即时领取物品` WHERE `玩家帐号`=? AND `流水号`=?");
      ps.setString(1, loginName.toLowerCase());
      ps.setInt(2, id);
      rs = ps.executeQuery();
      while (rs.next()) {
        int out = rs.getInt("领取状态");
        if (out != 0)
          return false; 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(co);
      SQLUtil.close(rs);
    } 
    return true;
  }
  
  public boolean update(String loginName, int id, String pcname, String ip) {
    if (!is_holding(loginName, id))
      return false; 
    Connection con = null;
    PreparedStatement pstm = null;
    try {
      Timestamp lastactive = new Timestamp(System.currentTimeMillis());
      con = DatabaseFactoryLogin.get().getConnection();
      String sqlstr = "UPDATE `w_玩家即时领取物品` SET `领取状态`=1,`领取玩家角色`=?,`领取时间`=?,`ip`=? WHERE `流水号`=? AND `玩家帐号`=?";
      pstm = con.prepareStatement("UPDATE `w_玩家即时领取物品` SET `领取状态`=1,`领取玩家角色`=?,`领取时间`=?,`ip`=? WHERE `流水号`=? AND `玩家帐号`=?");
      pstm.setString(1, pcname);
      pstm.setTimestamp(2, lastactive);
      pstm.setString(3, ip);
      pstm.setInt(4, id);
      pstm.setString(5, loginName);
      pstm.execute();
      return true;
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
    return false;
  }
}
